<?php 
/**
 * alltosun.com 市场人员无法处理的订单
 * ============================================================================
 * 版权所有 (C) 2009-2011 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址: http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 高昂 (gaoa@alltosun.com) $
 * $Date: 2016-4-30 下午2:37:25 $
 * $Id: $
 */
class Action
{
    private $per_page = 30;
    
    /**
     * 显示订单列表
     */
    public function index()
    {
        $page          = Request::Get('page_no', 1);
        $status        = Request::getParam('status', 1);
        $progress      = Request::getParam('progress', 5);
        $task_user_id  = Request::getParam('task_user_id', '-1');
        $type          = Request::getParam('type', 5);
        $search_filter = Request::getParam('search_filter', array());
        $is_excel      = Request::getParam('is_excel', 0);
        $test          = Request::getParam('test', 0);
        $is_only_ios   = Request::getParam('is_only_ios', 1);
    
        // 拼装搜索条件
        $is_empty = 0;
        $filter = array();
        if (isset($search_filter['id']) && $search_filter['id']) {
            $filter['id'] = $search_filter['id'];
        }
        if (isset($search_filter['progress']) && $search_filter['progress'] && $search_filter['progress'] != '-1') {
            $filter['progress'] = $search_filter['progress'];
        }
        if (isset($search_filter['order_code']) && $search_filter['order_code']) {
            $decoded_id = order_helper::order_id_decode($search_filter['order_code']);
            if(is_numeric($decoded_id)) {
                $filter['id'] = $decoded_id;
            }
        }
        // 获取当前登录人的角色
        $role_id = user_helper::get_admin_role_id();
        if (user_config::ASSISTANT == $role_id) {
            // 获取此助理的所有经销商的订单
            $dealer_ids = user_helper::get_assistant_dealer_ids();
            if ($dealer_ids && is_array($dealer_ids)) {
                $filter['agency_id'] = $dealer_ids;
            }
        }
    
        /**
         * 过滤所有涉及到测试经销商账号的订单去掉,但是如果登陆的是测试账号的助理,则可以看到
         */
        //         $filter_agency_ids = array();
        //         $filter_user_ids   = array();
        //         $assistant_user_ids = array();
        //         $filter_user_list = user_helper::get_user_list_by_mobile(user_config::$test_user_mobile);
        //         if ($filter_user_list) {
        //             foreach ($filter_user_list as $k => $v) {
        //                 array_push($filter_agency_ids, $v['id']);
        //                 array_push($filter_user_ids, $v['id']);
        //                 if ($v['assistant_user_id']) {
        //                     array_push($assistant_user_ids, $v['assistant_user_id']);
        //                 }
        //             }
        //             $cur_admin_id = user_helper::get_admin_id();
        //             if (!in_array($cur_admin_id, $assistant_user_ids) && $role_id != user_config::ASSISTANT) {
        //                 if ($filter_user_ids) {
        //                     $filter['`user_id` NOT ']   = $filter_user_ids;
        //                 }
        //                 if ($filter_agency_ids) {
        //                     $filter['`agency_id` NOT '] = $filter_agency_ids;
        //                 }
        //             }
        //         }
        if ($test != 1) {
            $filter['is_enable'] = 1;
        }
    
        if (isset($search_filter['mobile']) && $search_filter['mobile']) {
            $user_ids = user_helper::get_user_ids_by_mobile($search_filter['mobile']);
            $filter['user_id'] = $user_ids;
        }
    
        if (isset($search_filter['start_date']) && $search_filter['start_date']) {
            $filter['add_time >='] = $search_filter['start_date'].' 00:00:00';
        }
        if (isset($search_filter['end_date']) && $search_filter['end_date']) {
            $filter['add_time <='] = $search_filter['end_date'].' 23:59:59';
        }
        if (isset($progress) && $progress != '-1') {
            $filter['progress'] = intval($progress);
        }
    
        if ($status != -1) {
            $filter['status'] = $status;
        }
    
        // 代理
        if ($task_user_id == 1) {
            $filter['task_user_id > '] = 0;
        } else if ($task_user_id == 0) {
            $filter['task_user_id'] = 0;
        }
    
        if ($progress != '-1' || $status == 0) {
            $filter['status'] = $status;
        } else {
            $filter['1'] = 1;
        }
        // added type filter by ninghx 用来区分总部订单、经销商订单还是影院订单
        if (isset($type) && $type) {
            $filter['type'] = $type;
        }
    
        $all_marketing_ids      = _model('user_marketing')->getFields('marketing_id', array(1=>1));
        $filter['assigned_id'] = $all_marketing_ids;
        $filter['progress']    = array(1, 3);
        
        $ios_source_ids = _model('user_device')->getFields('user_id', array('`device_source` != '=>1));
        if ($is_only_ios) {
            $filter['`assigned_id` NOT '] = $ios_source_ids;
        }
        
        $order_list = array();
        if (!$is_empty) {
            $order_count = _model('order')->getTotal($filter);
            if ($order_count) {
                $pager = new Pager($this->per_page);
                if ($pager->generate($order_count)) {
                    Response::assign('pager', $pager);
                }
                $order_by = ' ORDER BY `id` DESC ';
                if(!$is_excel) {
                    $order_by .= $pager->getLimit($page);
                }
                $order_list = _model('order')->getList($filter, $order_by);
                foreach ($order_list as $k=>$v) {
                    $order_list[$k]['order_relation'] = _model('order_relation')->getList(array(
                        'order_id'=>$v['id'],
                        'status'  => 1
                    ));
                    $assigned_source_info = _uri('user_device', array('user_id'=>$v['assigned_id']));
                    if ($assigned_source_info) {
                        $order_list[$k]['source'] = $assigned_source_info['device_source'];
                        if (0 == $assigned_source_info['device_source']) {
                            $order_list[$k]['source_str'] = '其他系统';
                        } elseif (1 == $assigned_source_info['device_source']) {
                            $order_list[$k]['source_str'] = 'IOS';
                        } elseif (2 == $assigned_source_info['source']) {
                            $order_list[$k]['source_str'] = 'Android';
                        }
                    } else {
                        $order_list[$k]['source_str'] = '未知';
                    }
                    
                    $marketing_info = user_helper::get_user_info($v['assigned_id']);
                    $order_list[$k]['marketing_info'] = $marketing_info;
                }
    
                if($is_excel) {
                    $this->export_excel($order_list);
                }
                Response::assign('count', $order_count);
            }
        }
    
        $express_list = _model('express')->getList(array('status'=>1));
    
        Response::assign('role_id', $role_id);
        Response::assign('express_list', $express_list);
        Response::assign('order_list', $order_list);
        Response::assign('status', $status);
        Response::assign('progress', $progress);
        Response::assign('task_user_id', $task_user_id);
    
        Response::assign('type', $type);
        Response::assign('search_filter', $search_filter);
    
        Response::display('admin/marketing_order_list.html');
    }
}

?>